#!/bin/sh
#
# Autosupport Version 2.1.3
# Collect support information
#
# Copyright (C) 2005-2016, Digium, Inc.
#
# Written by John Bigelow (support@digium.com)
#            Charles Moye (cmoye@digium.com)
#            Trey Blancher (trey@digium.com)
#
# Distributed under the terms of the GNU General Public
# License
#
# usage:
# autosupport [prefix]
# ARGUMENTS:
# prefix - Prefix to tarball. (Referenced as $1)
#      Example: autosupport <Your Digium Technical Support Ticket number>

# Ensure the PATH variable includes '/usr/sbin' and '/sbin'
PATH=/usr/sbin:/sbin:${PATH}

DATE_STAMP=$(date +%F)
OUTPUT_FILE=digiuminfo.txt
TARBALL_OUTPUT_FILE="digium-info_${DATE_STAMP}.tar"
VAR_LIB_LIST_FILE="list-of-varlibasterisk.txt"
FILE_PREFIX=
FOLDER_PREFIX="${HOME}/"
files="n";
NONINTERACTIVE=0


# If a prefix is specified on command-line, add it.
if (set -u; : $1) 2> /dev/null
then
  case "$1" in
  -h | --help)
    echo
    echo "Digium autosupport script"
    echo "Copyright (C) 2005-2016, Digium, Inc."
    echo "Licensed under the terms of the GNU General Public License"
    echo
    echo "usage: autosupport [prefix]"
    echo "Valid Options:"
    echo "   [prefix]			Prefix to apply to output files"
    echo "				May be your existing Digium Technical Support Ticket ID"
    echo "   -n, --non-interactive	Run without requiring user input"
    echo "   -h, --help			Show help about options"
    echo
    echo "Example:"
    echo "    autosupport XXXXXXXX"
    echo "Generates:"
    echo "    XXXXXXXX_${OUTPUT_FILE}"
    echo "    XXXXXXXX_${TARBALL_OUTPUT_FILE}"
    echo
    exit
    ;;
  -n | --non-interactive)
    FILE_PREFIX=
    NONINTERACTIVE=1
    ;;
  *)
    FILE_PREFIX="${FILE_PREFIX}${1}_";
    ;;
  esac
fi

MYUID=$(id -u);

if [ $MYUID -ne  0 ]; then
  echo "You must be root to run this."
  exit 1
fi

SUPPORTED_MODULES="wcb4xxp wct4xxp wctc4xxp wctdm wctdm24xxp wcte11xp wcte12xp"
RELATED_MODULES="$SUPPORTED_MODULES dahdi_dummy dahdi_transcode dahdi_vpmadt032_loader";

OUTPUT_FILE="${FILE_PREFIX}${OUTPUT_FILE}"
TARBALL_OUTPUT_FILE="${FILE_PREFIX}${TARBALL_OUTPUT_FILE}"
VAR_LIB_LIST_FILE="${FILE_PREFIX}${VAR_LIB_LIST_FILE}"

OUTPUT="${FOLDER_PREFIX}${OUTPUT_FILE}"
TARBALL_OUTPUT="${FOLDER_PREFIX}${TARBALL_OUTPUT_FILE}"
VAR_LIB_LIST="${FOLDER_PREFIX}${VAR_LIB_LIST_FILE}"

# Done with setup, now start gathering information.

# function not implemented yet
determine_paths()
{
  ASTERISK_PROCESS=$(ps -o cmd -C asterisk | grep asterisk 2> /dev/null)
echo "detected process: $ASTERISK_PROCESS";
  # split on whitespace? or use sed/awk to parse
  # get first arg which will be path to Asterisk binary - add this to PATH
  # look for a '-C' if that is present, get the arg after it, and make the tarball collect that directory
  ## in addition to /etc/asterisk/ or instead of it?
}

collect_digiuminfo()
{
# Delete existing OUTPUT
[ -f $OUTPUT ] && rm -rf $OUTPUT

# Sanity Checks
echo "------------------" >> $OUTPUT;
echo "Sanity" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
md5sum $0 >> $OUTPUT;
grep "Autosupport Version" $0 | head -n1 >> $OUTPUT
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "Working";

echo "------------------" >> $OUTPUT;
echo "UPTIME : uptime" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
uptime >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "MEMORY : free" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
free >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "KERNEL VERSION : uname -a" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
uname -a >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "VERSION INFO : cat /proc/version" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
cat /proc/version >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "CMDLINE INFO : cat /proc/cmdline" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
cat /proc/cmdline >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."


if [ -d /sys/module/dahdi ]; then
  echo "------------------" >> $OUTPUT
  echo "dahdi version:" >> $OUTPUT
  echo "------------------" >> $OUTPUT
  echo "/sys/module/dahdi/version: " >> $OUTPUT
  cat /sys/module/dahdi/version 2> /dev/null >> $OUTPUT
  echo "" >> $OUTPUT;
  echo -n "."
fi

echo "------------------" >> $OUTPUT;
echo "DAHDI TOOLS : dahdi_cfg --help" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
dahdi_cfg --help 2>&1 | grep "Version" >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "DAHDI HARDWARE : dahdi_hardware" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
dahdi_hardware >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;

echo "------------------" >> $OUTPUT;
echo "ASTERISK INFO : asterisk -V" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
echo "asterisk -V:" >> $OUTPUT;
asterisk -V >> $OUTPUT;
echo >> $OUTPUT;
# Add check to see if asterisk is running.
if [ -e /var/run/asterisk.ctl ] || [ -e /var/run/asterisk/asterisk.ctl ]; then
  for command in "core show version" "pri show version" "dahdi show version" \
      "pjsip show version" "pjsip show buildopts" "pjproject show buildopts" \
      "core show translation" \
      "core show uptime" "core show settings" "core show sysinfo" "core show channels" \
      "pri show spans" "dahdi show status" "dahdi show channels" "dahdi show channel 1" \
      "pjsip show endpoints" "pjsip show registrations" "pjsip list channels" \
      "sip show peers" "sip show registry" "sip show channels" "sip show subscriptions" "sip show settings" \
      "show g729" "g729 show version" "g729 show licenses" "g729 show hostid" \
      "digium_phones show version" "digium_phones show alerts" "digium_phones show applications" \
      "digium_phones show firmwares" "digium_phones show lines" "digium_phones show networks" \
      "digium_phones show phones" "digium_phones show sessions" "digium_phones show settings" \
      "digium_phones show translations" ;
  do
    echo "asterisk -rx \"$command\"" >> $OUTPUT;
    asterisk -rx "$command" >> $OUTPUT;
    echo >> $OUTPUT;
    echo -n "."
  done
  echo >> $OUTPUT;
fi


for type in alerts applications firmwares lines networks phones translations;
do
	echo "------------------" >> $OUTPUT;
	echo "DIGIUM PHONE: $type " >> $OUTPUT;
	echo "------------------" >> $OUTPUT;
	TEMP=$(asterisk -rx "digium_phones show $type");
	if [ "$type" = "lines" ];
	then
		NUM=$(echo $TEMP | tail -n 1 | awk -F' ' '{print $(NF-4)}');
		if [ $NUM -gt 0 ];
		then
			for l in $(echo $TEMP | sed 's/^--- [[:alpha:]]\+ --- //;
				s/Lines \(Un-\)\?Mapped to Phones --- //g;
				s/ Internal//g;
				s/---- [a-zA-Z0-9 ]\+ ----$//;
				s/--- //g');
			do
				asterisk -rx "digium_phones show line $l" >> $OUTPUT;
				echo "--------" >> $OUTPUT;
				echo >> $OUTPUT;
			done
		else
			echo "No configurations of type $type..." >> $OUTPUT;
			echo >> $OUTPUT;
		fi;
	elif [ "$type" = "firmwares" ];
	then
		for f in $(echo $TEMP | sed 's/--- Configured Firmware Options --- //');
		do
			asterisk -rx "digium_phones show firmware $f" >> $OUTPUT;
			echo "--------" >> $OUTPUT;
			echo >> $OUTPUT;
		done
	elif [ "$type" = "translations" ];
	then
		for t in $(echo $TEMP | sed 's/--- Translation tables ---//');
		do
			asterisk -rx "digium_phones show translation $t"
		done
	else
		NUM=$(echo $TEMP | tail -n 1 | awk -F' ' '{print $(NF-3)}');
		if [ $NUM -gt 0 ];
		then
			for t in $(echo $TEMP | sed 's/^--- [[:alpha:]]\+ --- //;
																	 s/---- [a-zA-Z0-9 ]\+ ----$//
																	 s/---- Digium Phones ---- //');
			do
				asterisk -rx "digium_phones show $(echo $type | sed 's/s\b//') $t" >> $OUTPUT;
				echo "--------" >> $OUTPUT;
				echo >> $OUTPUT;
			done;
		else
			echo "No configurations of type $type..." >> $OUTPUT;
			echo >> $OUTPUT;
		fi;
	fi;
done;
echo >> $OUTPUT;


echo "------------------" >> $OUTPUT;
echo "OTHER INFO : cat /etc/*{issue,release,version}*" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
for file in $(ls /etc/*issue* /etc/*release* /etc/*version* \
  /etc/*motd* /etc/asterisknow-version /dev/zap/ /dev/dahdi/ \
  2> /dev/null); do
  if [ -f $file ]; then
    echo "$file:" >> $OUTPUT;
    cat $file >> $OUTPUT;
    echo >> $OUTPUT;
    echo -n "."
  fi
done
echo >> $OUTPUT;

echo "------------------" >> $OUTPUT;
echo "TOP : top -bn1" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
top -bn1 >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "RUNNING PROCESSES : ps aux" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
ps aux >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "INTERRUPTS : cat /proc/interrupts" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
cat /proc/interrupts >> $OUTPUT;
echo -n "."
sleep 2;
echo "---------- sleep(2) ----------" >> $OUTPUT;
cat /proc/interrupts >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "DAHDI SCAN : dahdi_scan" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
dahdi_scan >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "----------------------------" >> $OUTPUT;
echo "CAT OF DAHDI CHANNELS : cat /proc/dahdi/" >> $OUTPUT;
echo "----------------------------" >> $OUTPUT;
if [ -d /proc/dahdi/ ]; then
  for file in $(ls /proc/dahdi/ 2> /dev/null); do
    echo "----------------------------" >> $OUTPUT;
    echo "/proc/dahdi/$file:" >> $OUTPUT;
    cat /proc/dahdi/$file >> $OUTPUT;
    echo >> $OUTPUT;
    echo -n "."
  done
fi
echo >> $OUTPUT;

echo "------------------" >> $OUTPUT;
echo "DMESG OUTPUT : dmesg" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
dmesg >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "LOADED MODULES : lsmod" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
lsmod >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

# Grab the parameters for each module
for mod in $SUPPORTED_MODULES; do
  if [ -d /sys/module/$mod ]; then
    echo "------------------" >> $OUTPUT
    echo "$mod parameters" >> $OUTPUT
    echo "------------------" >> $OUTPUT
    echo "/sys/module/$mod/parameters:" >> $OUTPUT
    for param in $(ls /sys/module/$mod/parameters/ /sys/module/$mod/ 2> /dev/null); do
      if [ -f /sys/module/$mod/parameters/$param ]; then
        echo -n "$param:  " >> $OUTPUT
        cat /sys/module/$mod/parameters/$param 2> /dev/null >> $OUTPUT
      elif [ -f /sys/module/$mod/$param ]; then
        # Handle Zaptel doing it differently
        echo -n "$param:  " >> $OUTPUT
        cat /sys/module/$mod/$param 2> /dev/null >> $OUTPUT
      fi
      echo -n "."
    done
    echo >> $OUTPUT
  fi
done

echo "------------------------" >> $OUTPUT;
echo "DAHDI MODULE INFO : modinfo" >> $OUTPUT;
echo "------------------------" >> $OUTPUT;
for file in $(ls /lib/modules/$(uname -r)/dahdi/*.ko \
  /lib/modules/$(uname -r)/dahdi/*/*.ko\
  /lib/modules/$(uname -r)/extra/*.ko \
  /lib/modules/$(uname -r)/extra/*/*.ko \
  /lib/modules/$(uname -r)/misc/*.ko \
  /lib/modules/$(uname -r)/misc/*/*.ko 2> /dev/null); do
  if [ -f $file ]; then
    echo "------------------------" >> $OUTPUT;
    modinfo $file >> $OUTPUT;
    echo >> $OUTPUT;
    echo -n "."
  fi
done
echo "------------------------" >> $OUTPUT;
echo >> $OUTPUT;

echo "------------------" >> $OUTPUT;
echo "PCI LIST : lspci -vvvb" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
lspci -vvvb >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "PCI LIST(no lookup) : lspci -vvvbn" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
lspci -vvvbn >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "CPU INFO : cat /proc/cpuinfo" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
cat /proc/cpuinfo >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "----------------------" >> $OUTPUT;
echo "VPM FIRMWARE INSTALLED : ls -la /lib/firmware" >> $OUTPUT;
echo "----------------------" >> $OUTPUT;
$(ls -la /lib/firmware 2> /dev/null >> $OUTPUT);
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "NETWORK INFO : route -n; iptables -L; ifconfig -a" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
route -n 2> /dev/null >> $OUTPUT;
echo >> $OUTPUT;
echo "------------------" >> $OUTPUT;
iptables -L 2> /dev/null >> $OUTPUT;
echo >> $OUTPUT;
echo "------------------" >> $OUTPUT;
ifconfig -a 2> /dev/null >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "DMIDECODE : dmidecode" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
dmidecode >> $OUTPUT;
echo >> $OUTPUT;
echo >> $OUTPUT;
echo -n "."

echo "------------------" >> $OUTPUT;
echo "KERNEL CONFIG : cat /path/.config" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
for file in /lib/modules/$(uname -r)/build/.config /usr/src/linux/.config; do
  if [ -f $file ]; then
    echo "------------------" >> $OUTPUT;
    echo "$file:" >> $OUTPUT;
    cat $file >> $OUTPUT;
    echo >> $OUTPUT;
    echo -n "."
  fi
done
echo >> $OUTPUT;

FIRMWARE_DIR=`grep firmware_package_directory /etc/asterisk/res_digium_phone.conf|sed 's/;.*$//;'|grep firmware|sed 's/firmware_package_directory=//;'`;
if [ `echo $FIRMWARE_DIR|egrep -v '^$'|wc -l` -eq "0" ]
then
  FIRMWARE_DIR="/var/www/firmware_package_directory"
fi
echo "------------------" >> $OUTPUT;
echo "FIRMWARE LISTING: ls -al $FIRMWARE_DIR" >> $OUTPUT;
echo "------------------" >> $OUTPUT;
ls -al $FIRMWARE_DIR >> $OUTPUT;
echo >> $OUTPUT;

FIRMWARE_URLS=`grep file_url_prefix /etc/asterisk/res_digium_phone.conf|sed 's/;.*$//;'|grep file|sed 's/file_url_prefix=//;'`;
for FIRMWARE_URL in $FIRMWARE_URLS; do
  echo "------------------" >> $OUTPUT;
  echo "REMOTE FIRMWARE LISTING: wget $FIRMWARE_URL" >> $OUTPUT;
  echo "------------------" >> $OUTPUT;
  wget "$FIRMWARE_URL" -O- 2>/dev/null >> $OUTPUT;
  echo >> $OUTPUT;
done

echo "done!"
}

collect_var_lib_list()
{
  # Delete existing VAR_LIB_LIST
  [ -f $VAR_LIB_LIST ] && rm -rf $VAR_LIB_LIST
  DIR_LISTING="/var/lib/asterisk/"
  if [ -d /var/lib/digium ]; then
    DIR_LISTING="/var/lib/digium/ ${DIR_LISTING}"
  fi
  if [ -d /usr/lib/asterisk/modules/ ]; then
    DIR_LISTING="/usr/lib/asterisk/modules/ ${DIR_LISTING}"
  fi
  echo "ls -1aAR $DIR_LISTING > $VAR_LIB_LIST"
  $(ls -1aR ${DIR_LISTING} | sed -e '/^\.\.*$/d' > $VAR_LIB_LIST);
}

collect_config_backup()
{
  collect_var_lib_list
  # Include the /etc/asterisk directory, modprobe.conf, and the modprobe.d directory
  TAR_FILES="/etc/asterisk/ /etc/modprobe.*"

  # Check if any Asterisk licenses are installed
  # G.729, Fax, ABE, Cepstral, Skype, etc.
  if [ -d /var/lib/asterisk/licenses/ ]; then
    TAR_FILES="$TAR_FILES /var/lib/asterisk/licenses/*"
  fi

  # Check if any Digium licenses are installed
  # HPEC
  if [ -d /var/lib/digium/licenses/ ]; then
    TAR_FILES="$TAR_FILES /var/lib/digium/licenses/*"
  fi

  # Check if DAHDI is installed
  if [ -d /etc/dahdi ]; then
    TAR_FILES="$TAR_FILES /etc/dahdi*"
  fi

  # Grab the dahdi init scripts, in case they have been modified
  if [ -f /etc/init.d/dahdi ]; then
    TAR_FILES="$TAR_FILES /etc/init.d/dahdi"
  fi

  # Check for fxotune.conf
  if [ -f /etc/fxotune.conf ]; then
    TAR_FILES="$TAR_FILES /etc/fxotune.conf*"
  fi

  # Check for misdn-init.conf
  if [ -f /etc/misdn-init.conf ]; then
    TAR_FILES="$TAR_FILES /etc/misdn-init.conf*"
  fi

  # Check for digiuminfo.txt
  if [ -f $HOME/$OUTPUT_FILE ]; then
    TAR_FILES="$TAR_FILES $OUTPUT_FILE"
  fi

  # Check for asterisk listing
  if [ -f $VAR_LIB_LIST ]; then
    TAR_FILES="$TAR_FILES $VAR_LIB_LIST_FILE"
  fi

  # Collect System Log Files
  if [ -f /var/log/dmesg ]; then
    TAR_FILES="$TAR_FILES /var/log/dmesg"
  fi
  if [ -f /var/log/messages ]; then
    TAR_FILES="$TAR_FILES /var/log/messages"
  fi
  if [ -f /var/log/syslog ]; then
    TAR_FILES="$TAR_FILES /var/log/syslog"
  fi

  [ -f $TARBALL_OUTPUT ] && rm -rf $TARBALL_OUTPUT
  [ -f $TARBALL_OUTPUT.gz ] && rm -rf $TARBALL_OUTPUT.gz

  echo "tarring: tar -chvf ${TARBALL_OUTPUT} $TAR_FILES"
  cd $HOME && tar -chvf $TARBALL_OUTPUT $TAR_FILES
  echo "gzipping $TARBALL_OUTPUT"
  gzip $TARBALL_OUTPUT
}

collect()
{
  echo "This may take up to half a minute to run. Please be patient."
  collect_digiuminfo;
  collect_config_backup;

  # ensure the output was written
  if [ -f $TARBALL_OUTPUT.gz ]; then
    clear;
    echo;
    echo;
    echo "Tarball has been stored to:"
    echo "$TARBALL_OUTPUT.gz"
    echo "Please send this file to an email case you already"
    echo "have open with Digium Tech Support."
  else
    echo;
    echo;
    echo "An error has occurred in capturing information."
    echo "$TARBALL_OUTPUT.gz was not found."
    echo;
    echo "Please investigate your system, and send the output"
    echo "above to your Digium Technical Support Ticket."
  fi
}

clear;

if [ $NONINTERACTIVE -eq 1 ]; then
  collect;
else
  echo
  echo "This script will try to collect the following pieces of"
  echo "information from your system."
  echo
  echo "1. Information about your system such as:"
  echo "pci listing, dmesg, running processes, and kernel version"
  echo
  echo "2. A backup of elements of your configuration such as:"
  echo "asterisk config files, license files, loaded dahdi module"
  echo "parameters, and other asterisk/dahdi related files."
  echo
  echo "Collect this information [y/n] ? "
  read files;

  if [ "$files" = "y" ] || [ "$files" = "yes" ]; then
    collect;
  else
    clear;
    echo;
    echo "User selected not to continue."
  fi
fi
exit
